﻿Imports System.Data.SqlClient
Public Class XoaLop

    Private Property Lop_ID As Object

    Private Sub XoaLop_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = Ketnoi.str
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.Connection = con
        cmd.CommandText = "SELECT * FROM Lop WHERE Lop_ID='" + MaLop + "'"
        Dim rd As SqlDataReader
        rd = cmd.ExecuteReader()
        Dim td As DataTable = New DataTable()
        td.Load(rd)
        Me.txtMaLop.Text = td.Rows(0)(0).ToString()
        Me.txtTenLop.Text = td.Rows(0)(1).ToString()
        Dim MaKhoa As String
        MaKhoa = td.Rows(0)(2).ToString()
        cmd.CommandText = "SELECT TenKhoa FROM Khoa WHERE Khoa_ID='" + MaKhoa + "'"
        Dim rd1 As SqlDataReader
        rd1 = cmd.ExecuteReader()
        Dim td1 As DataTable = New DataTable()
        td1.Load(rd1)
        Me.txtKhoa.Text = td1.Rows(0)(0).ToString()
        con.Close()

    End Sub

    Private Sub btnXoa_Click(sender As System.Object, e As System.EventArgs) Handles btnXoa.Click
        Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = Ketnoi.str
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand()
        Dim cmd1 As SqlCommand = New SqlCommand()
        Dim cmd2 As SqlCommand = New SqlCommand()
        Dim cmd3 As SqlCommand = New SqlCommand()
        cmd.Connection = con
        cmd1.Connection = con
        cmd2.Connection = con
        cmd3.Connection = con
        cmd1.CommandText = "SELECT SinhVien_ID FROM SinhVien WHERE ID_Lop='" + MaLop + "'"
        Dim rd As SqlDataReader
        rd = cmd1.ExecuteReader()
        Dim td As DataTable = New DataTable()
        td.Load(rd)
        Dim MaSinhVien As String
        If td.Rows.Count = 0 Then
            MaSinhVien = ""
        Else
            MaSinhVien = td.Rows(0)(0).ToString()
        End If
        cmd2.CommandText = "DELETE FROM KetQua WHERE ID_SinhVien='" + MaSinhVien + "' "
        cmd3.CommandText = "DELETE FROM SinhVien WHERE SinhVien_ID='" + MaSinhVien + "' AND ID_Lop='" + MaLop + "'"
        cmd.CommandText = "DELETE FROM Lop WHere Lop_ID='" + MaLop + "'"
        Dim result As DialogResult
        result = MessageBox.Show("BẠN CÓ MUỐN XOÁ DỮ LIỆU KHÔNG?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = DialogResult.Yes Then
            cmd2.ExecuteNonQuery()
            cmd3.ExecuteNonQuery()
            cmd.ExecuteNonQuery()
            con.Close()
            MessageBox.Show("DỮ LIỆU ĐÃ ĐƯỢC XOÁ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
            Me.Close()
            Dim frm As DanhSachLop = New DanhSachLop(Lop_ID)
            frm.Show()

        End If

    End Sub

    Private Sub btnThoat_Click(sender As System.Object, e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
        Dim frm As DanhSachLop = New DanhSachLop(Lop_ID)
        frm.Show()

    End Sub

    Private Function MaLop() As String
        Throw New NotImplementedException
    End Function

End Class